﻿using System;
using WinRTFramework.Common;
using WinRTFramework.DefaultImplementations;
using WinRTFramework.Interfaces.Contracts.Sharing;

namespace ShareSource.ViewModels
{
    class ShareLinkViewModel : DefaultPageViewModel, ISupportSharingSourceContract
    {
        private string _sharingText;
        public string SharingText
        {
            get { return _sharingText; }
            set { _sharingText = value; NotifyPropertyChanged(() => SharingText); }
        }

        private DelegateCommand _shareCommand;
        public DelegateCommand ShareCommand
        {
            get { return _shareCommand = _shareCommand ?? new DelegateCommand(OnShare); }
        }

        private void OnShare()
        {
            SharingService.ShowShareUI();
        }

        #region ISupportSharingSourceContract
        public void OnShareRequested(Windows.ApplicationModel.DataTransfer.DataRequest dataRequest)
        {
            if(string.IsNullOrEmpty(SharingText))
            {
                dataRequest.FailWithDisplayText("No Link provided!");
                return;
            }

            Uri uri;
            if(!Uri.TryCreate(SharingText, UriKind.Absolute, out uri))
            {
                dataRequest.FailWithDisplayText("No valid Link provided!");
                return;
            }

            dataRequest.Data.Properties.Title = "Link Sharing Example";
            dataRequest.Data.SetUri(uri);
        }

        public ISharingService SharingService { get; set; }
        #endregion
    }
}
